package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.annotation.Anonymous;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduTeacherVo;
import com.ruoyi.edu.domain.bo.EduTeacherBo;
import com.ruoyi.edu.service.IEduTeacherService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 讲师管理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/edu/teacher")
public class EduTeacherController extends BaseController {

    private final IEduTeacherService iEduTeacherService;

    /**
     * 查询讲师管理列表
     */
    @SaCheckPermission("edu:teacher:list")
    @GetMapping("/list")
    public TableDataInfo<EduTeacherVo> list(EduTeacherBo bo, PageQuery pageQuery) {
        return iEduTeacherService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出讲师管理列表
     */
    @SaCheckPermission("edu:teacher:export")
    @Log(title = "讲师管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduTeacherBo bo, HttpServletResponse response) {
        List<EduTeacherVo> list = iEduTeacherService.queryList(bo);
        ExcelUtil.exportExcel(list, "讲师管理", EduTeacherVo.class, response);
    }

    /**
     * 获取讲师管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:teacher:query")
    @GetMapping("/{id}")
    public R<EduTeacherVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduTeacherService.queryById(id));
    }
    @GetMapping("queryAll")
    @Anonymous
    public R queryAll(){
        return iEduTeacherService.query();
    }
    /**
     * 新增讲师管理
     */
    @SaCheckPermission("edu:teacher:add")
    @Log(title = "讲师管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduTeacherBo bo) {
        return toAjax(iEduTeacherService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改讲师管理
     */
    @SaCheckPermission("edu:teacher:edit")
    @Log(title = "讲师管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduTeacherBo bo) {
        return toAjax(iEduTeacherService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除讲师管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:teacher:remove")
    @Log(title = "讲师管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEduTeacherService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
